package com.example.myweather;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Contacts;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.myweather.entity.City;
import com.example.myweather.service.WeatherManager;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class CityFragment extends Fragment {

    static City city;

    TextView city1;

    public CityFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_moregement, container, false);
         city1 = view.findViewById(R.id.textView2);
        TextView city2 = view.findViewById(R.id.textView4);
        TextView city3 = view.findViewById(R.id.textView10);
        TextView city4 = view.findViewById(R.id.textView11);
        TextView city5 = view.findViewById(R.id.textView12);
        TextView city6 = view.findViewById(R.id.textView13);
        TextView city7 = view.findViewById(R.id.textView14);
        TextView city8 = view.findViewById(R.id.textView15);
        TextView city9 = view.findViewById(R.id.textView16);
        TextView city10 = view.findViewById(R.id.textView17);
        TextView city11 = view.findViewById(R.id.textView18);
        TextView city12 = view.findViewById(R.id.textView19);
        TextView city13 = view.findViewById(R.id.textView44);
        TextView city14 = view.findViewById(R.id.textView43);
        TextView city15 = view.findViewById(R.id.textView8);
        TextView city16 = view.findViewById(R.id.textView9);
        TextView city17 = view.findViewById(R.id.textView20);
        TextView city18 = view.findViewById(R.id.textView21);
        TextView city19 = view.findViewById(R.id.textView22);
        TextView city20 = view.findViewById(R.id.textView27);
        TextView city21 = view.findViewById(R.id.textView28);
        TextView city22 = view.findViewById(R.id.textView29);
        TextView city23 = view.findViewById(R.id.textView26);
        TextView city24 = view.findViewById(R.id.textView23);
        TextView city25 = view.findViewById(R.id.textView24);
        TextView city26 = view.findViewById(R.id.textView25);
        TextView city27 = view.findViewById(R.id.textView30);
        TextView city28 = view.findViewById(R.id.textView31);
        TextView city29 = view.findViewById(R.id.textView32);
        TextView city30 = view.findViewById(R.id.textView33);
        TextView city31 = view.findViewById(R.id.textView35);
        TextView city32 = view.findViewById(R.id.textView36);
        TextView city33 = view.findViewById(R.id.textView34);
        TextView city35 = view.findViewById(R.id.textView40);
        TextView city36 = view.findViewById(R.id.textView37);
        TextView city37 = view.findViewById(R.id.textView39);
        TextView city38 = view.findViewById(R.id.textView41);
        TextView city39 = view.findViewById(R.id.textView42);



        ArrayList<TextView> citys = new ArrayList<>(12);
        citys.add(city1);
        citys.add(city2);
        citys.add(city3);
        citys.add(city4);
        citys.add(city5);
        citys.add(city6);
        citys.add(city7);
        citys.add(city8);
        citys.add(city9);
        citys.add(city10);
        citys.add(city11);
        citys.add(city12);
        citys.add(city13);
        citys.add(city14);
        citys.add(city15);
        citys.add(city16);
        citys.add(city17);
        citys.add(city18);
        citys.add(city19);
        citys.add(city20);
        citys.add(city21);
        citys.add(city22);
        citys.add(city23);
        citys.add(city24);
        citys.add(city25);
        citys.add(city26);
        citys.add(city27);
        citys.add(city28);
        citys.add(city29);
        citys.add(city30);
        citys.add(city31);
        citys.add(city32);
        citys.add(city33);

        citys.add(city35);
        citys.add(city36);
        citys.add(city37);
        citys.add(city38);
        citys.add(city39);








        for (int i = 0; i < citys.size(); i++) {
            String city = citys.get(i).getText().toString();
            citys.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Weatherfragment wf = new Weatherfragment();
                    wf.setSelectCity(city);
                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.more,wf)
                            .addToBackStack(null)
                            .commit();
                    ReadCityData readCityData = new ReadCityData();
                    readCityData.execute(city);
                    Intent intent = new Intent();
                    intent.putExtra("city",city);
                }

            });

            }
            return view;


}

    class ReadCityData extends AsyncTask<String, Void, City> {
        @Override
        protected City doInBackground(String... strings) {
            WeatherManager weatherManager = new WeatherManager();

            try {
                Log.d("test", "" + weatherManager.getCitynm(strings[0]));
                return weatherManager.getCitynm(strings[0]);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }


        @Override
        protected void onPostExecute(City c) {

            city = c;

                    }
    }

}